//
//  CustomViewController.m
//  IPTV
//
//  Created by liao Echo on 13-12-29.
//  Copyright (c) 2013年 Echo. All rights reserved.
//

#import "CustomViewController.h"
#import "DataManager.h"
#import "PokerControlClient.h"

@interface CustomViewController ()

@end

@implementation CustomViewController
@synthesize delegate, selDeveceIndex;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        array = [[NSMutableArray alloc] init];
    }
    return self;
}
-(void)rightBtn{
    for (UIView *view in [self.view subviews]) {
        if (view.tag == bg_tag) {
            view.hidden = NO;
        }
    }
    add = YES;
}
-(void)btnBgPressed{
    for (UIView *view in [self.view subviews]) {
        if (view.tag == bg_tag) {
            view.hidden = YES;
        }
    }
    add = NO;
}
-(void)save{
    if (iseditEd) {
        [deviceInfo.keyArray removeAllObjects];
        for (NSDataInfo *dataInfo in array) {
            [deviceInfo.keyArray addObject:dataInfo];
        }
        [[DataManager GetInstance:[PokerControlClient share].net_deviceName] setAllDevices];
    }
    [delegate editEd];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = STR(@"自定义");
    if ([Global curLanguageEn]) {
        self.title = @"Custom";
    }
    
    if (selDeveceIndex < [DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray.count) {
        deviceInfo = [[DataManager GetInstance:[PokerControlClient share].net_deviceName].deviceArray objectAtIndex:selDeveceIndex];
    }
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem backImageBarButtonItem:[UIImage imageNamed:@"btn_save.png"] target:self action:@selector(save)];
    
    right = [GoodBtn btnWithImg:@"custom_add.png"];
    [right addTarget:self action:@selector(rightBtn) forControlEvents:UIControlEventTouchUpInside];
    right.frame = CGRectMake(SCREEN_WIDTH-50, 0, 50, 50);
    [self.view addSubview:right];
    
    bg = [[UIView alloc] init];
    bg.backgroundColor = [UIColor colorWithRed:170/255.0 green:170/255.0 blue:170/255.0 alpha:1];
    bg.tag = bg_tag;
    bg.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-20-320-44, 320, 320);
    [self.view addSubview:bg];
    
    btnBg = [[GoodBtn alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-FH(bg)-64)];
    btnBg.tag = bg_tag;
    btnBg.backgroundColor = RGB(0, 0, 0, .4);
    [btnBg addTarget:self action:@selector(btnBgPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnBg];
    
    for (int i=0; i<5; i++) {
        for (int j=0; j<4; j++) {
            MyDraggableImage *view = [[MyDraggableImage alloc] init];
            view.contentMode = UIViewContentModeScaleToFill;
            view.tag = bg_tag;
            view.delegate = self;
            view.userInteractionEnabled = YES;
            [self.view addSubview:view];
            if (i == 0) {
                int top = 12;
                view.frame = CGRectMake(j*80, FY(bg)+top+i*60, 45, 45);
                if (j == 0) {
                    view.frame = CGRectMake(j*80+20, FY(bg)+top+i*60, 45, 45);
                    view.name = @"custom_left";
                    view.image = [UIImage imageNamed:@"custom_left_2.png"];
                }else if (j == 1) {
                    view.frame = CGRectMake(j*80+20, FY(bg)+top+i*60, 45, 45);
                    view.name = @"custom_right";
                    view.image = [UIImage imageNamed:@"custom_right_2.png"];
                }else if (j == 2) {
                    view.name = @"custom_up";
                    view.image = [UIImage imageNamed:@"custom_up_2.png"];
                }else if (j == 3) {
                    view.name = @"custom_down";
                    view.image = [UIImage imageNamed:@"custom_down_2.png"];
                }
            }else if (i == 1) {
                int top = 8;
                if (j == 0) {
                    view.name = @"custom_ok";
                    view.image = [UIImage imageNamed:@"custom_ok_2.png"];
                    view.frame = CGRectMake(j*80+25, FY(bg)+8+i*60+top, 45, 45);
                }else if (j == 1) {
                    view.name = @"open";
                    view.frame = CGRectMake(j*80+15, FY(bg)+8+i*60+top, 50, 50);
                    view.image = [UIImage imageNamed:@"open_2.png"];
                }else if (j == 2) {
                    view.name = @"custom_plus";
                    view.frame = CGRectMake(j*80+6, FY(bg)+8+i*60, 35, 57);
                    view.image = [UIImage imageNamed:@"custom_plus_2.png"];
                }else if (j == 3) {
                    view.name = @"custom_reduce";
                    view.frame = CGRectMake(j*80+2, FY(bg)+8+i*60, 35, 57);
                    view.image = [UIImage imageNamed:@"custom_reduce_2.png"];
                }
            }else if (i == 2) {
                int top = 15;
                if (j == 0) {
                    view.name = @"custom_volume_large";
                    view.frame = CGRectMake(j*80+20, FY(bg)+8+i*60+top, 60, 35);
                    view.image = [UIImage imageNamed:@"custom_volume_large_2.png"];
                }else if (j == 1) {
                    view.name = @"custom_volume_small";
                    view.frame = CGRectMake(j*80+10, FY(bg)+8+i*60+top, 60, 35);
                    view.image = [UIImage imageNamed:@"custom_volume_small_2.png"];
                }else if (j == 2) {
                    view.name = @"custom_button";
                    view.frame = CGRectMake(j*80, FY(bg)+8+i*60+5, 50, 50);
                    view.image = [UIImage imageNamed:@"custom_button_2.png"];
                    
                    view.type = 1;
                    [view setTitleColor:RGB(155, 220, 0, 1) forState:UIControlStateNormal];
                    view.titleLabel.font = [UIFont systemFontOfSize:13];
                    [view setTitle:@"1" forState:UIControlStateNormal];
                }else if (j == 3) {
                    view.name = @"custom_button";
                    view.frame = CGRectMake(j*80-10, FY(bg)+8+i*60+top, 70, 30);
                    view.image = [UIImage imageNamed:@"custom_button_1.png"];
                    
                    view.type = 2;
                    [view setTitleColor:RGB(155, 220, 0, 1) forState:UIControlStateNormal];
                    view.titleLabel.font = [UIFont systemFontOfSize:13];
                    [view setTitle:STR(@"自定义") forState:UIControlStateNormal];
                    if ([Global curLanguageEn]) {
                        [view setTitle:@"Custom" forState:UIControlStateNormal];
                    }
                }
            }else if (i == 3) {
                int top = 15;
                if (j == 0) {
                    view.name = @"custom_off";
                    view.frame = CGRectMake(j*80+30, FY(bg)+8+i*60+top, 80/2, 80/2);
                    view.image = [UIImage imageNamed:@"custom_off_2.png"];
                }else if (j == 1) {
                    view.name = @"custom_on";
                    view.frame = CGRectMake(j*80+20, FY(bg)+8+i*60+top, 80/2, 80/2);
                    view.image = [UIImage imageNamed:@"custom_on_2.png"];
                }else if (j == 2) {
                    view.name = @"up_button";
                    view.frame = CGRectMake(j*80+8, FY(bg)+8+i*60+top-5, 40, 57);
                    view.image = [UIImage imageNamed:@"up_button_2.png"];
                }else if (j == 3) {
                    view.name = @"down_button";
                    view.frame = CGRectMake(j*80+5, FY(bg)+8+i*60+top-4, 40, 57+5);
                    view.image = [UIImage imageNamed:@"down_button_2.png"];
                }
            }else if (i == 4) {
                int top = 17;
                if (j == 0) {
                    view.name = @"mute_button";
                    view.frame = CGRectMake(j*80+25, FY(bg)+8+i*60+top-3, 50, 50);
                    view.image = [UIImage imageNamed:@"mute_button_2.png"];
                }else if (j == 1) {
                    view.name = @"tv_home_button";
                    view.frame = CGRectMake(j*80+20, FY(bg)+8+i*60+top, 80/2, 80/2);
                    view.image = [UIImage imageNamed:@"tv_home_button_2.png"];
                }else if (j == 2) {
                    view.name = @"back_button";
                    view.frame = CGRectMake(j*80+10, FY(bg)+8+i*60+top, 80/2, 80/2);
                    view.image = [UIImage imageNamed:@"back_button_2.png"];
                }else if (j == 3) {
                    view.name = @"AV_button";
                    view.frame = CGRectMake(j*80+5, FY(bg)+8+i*60+top, 80/2, 80/2);
                    view.image = [UIImage imageNamed:@"AV_button_2.png"];
                }
            }
        }
    }
    [self btnBgPressed];
	// Do any additional setup after loading the view.
    for (int i=0; i<deviceInfo.keyArray.count; i++) {
        NSDataInfo *dataInfo = [deviceInfo.keyArray objectAtIndex:i];
        NSString *str = [NSString stringWithFormat:@"%@_1", dataInfo.name];
        MyDraggableImage *btn = [[MyDraggableImage alloc] init];
        btn._id = dataInfo._id;
        btn.tag = bg_tag_save+i;
        btn.delegate = self;
        btn.image = [UIImage imageNamed:str];
        btn.frame = CGRectMake(dataInfo.frame_x, dataInfo.frame_y, dataInfo.frame_w, dataInfo.frame_h);
        [self.view insertSubview:btn belowSubview:bg];
        
        if (dataInfo.type == 1) {
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setTitle:@"1" forState:UIControlStateNormal];
        }else if (dataInfo.type == 2) {
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setTitle:STR(@"自定义") forState:UIControlStateNormal];
            if ([Global curLanguageEn]) {
                [btn setTitle:@"Custom" forState:UIControlStateNormal];
            }
        }
        
        NSDataInfo *dataInfo1 = [[NSDataInfo alloc] init];
        dataInfo1.isCustom = YES;
        dataInfo1.name = dataInfo.name;
        dataInfo1._id = dataInfo._id;
        dataInfo1.frame_x = dataInfo.frame_x;
        dataInfo1.frame_y = dataInfo.frame_y;
        dataInfo1.frame_w = dataInfo.frame_w;
        dataInfo1.frame_h = dataInfo.frame_h;
        dataInfo1.type = dataInfo.type;
        
        [array addObject:dataInfo1];
    }
}
-(void)clickButtonEnd:(CGRect)rect name:(NSString*)name view:(MyDraggableImage *)view{
    
    if (add) {

        MyDraggableImage *btn = [[MyDraggableImage alloc] init];
        btn._id = [[IDGenerator share] genID];
        btn.delegate = self;
        btn.type = view.type;
        btn.tag = bg_tag_save+array.count;
        NSString *str = [NSString stringWithFormat:@"%@_1", name];
        btn.image = [UIImage imageNamed:str];
        btn.frame = rect;
        [self.view insertSubview:btn belowSubview:bg];
        
        if (btn.type == 1) {
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setTitle:@"1" forState:UIControlStateNormal];
        }else if (btn.type == 2) {
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setTitle:STR(@"自定义") forState:UIControlStateNormal];
            if ([Global curLanguageEn]) {
                [btn setTitle:@"Custom" forState:UIControlStateNormal];
            }
        }
        
        if(deviceInfo.type == DEVICE_CUSTOM) {//自定义
            NSDataInfo *dataInfo = [[NSDataInfo alloc] init];
            dataInfo.isCustom = YES;
            dataInfo.name = name;
            dataInfo._id = btn._id;
            dataInfo.frame_x = rect.origin.x;
            dataInfo.frame_y = rect.origin.y;
            dataInfo.frame_w = rect.size.width;
            dataInfo.frame_h = rect.size.height;
            dataInfo.type = btn.type;
            
            [array addObject:dataInfo];
            iseditEd = YES;
        }
    }else{
        int gap = 70;
        //删除按钮
        if (view.frame.origin.x>SCREEN_WIDTH-gap && view.frame.origin.y<gap) {
            [view removeFromSuperview];
            for (int i=0; i<array.count; i++) {
                NSDataInfo *dataInfo = [array objectAtIndex:i];
                if ([dataInfo._id isEqualToString:view._id]) {
                    [array removeObjectAtIndex:i];
                    iseditEd = YES;
                    break;
                }
            }
        }else{//移动位置
            for (int i=0; i<array.count; i++) {
                NSDataInfo *dataInfo = [array objectAtIndex:i];
                if ([dataInfo._id isEqualToString:view._id]) {
                    dataInfo.frame_x = rect.origin.x;
                    dataInfo.frame_y = rect.origin.y;
                    dataInfo.frame_w = rect.size.width;
                    dataInfo.frame_h = rect.size.height;
                    iseditEd = YES;
                    break;
                }
            }
        }
        [right setBackgroundImage:IMG(@"custom_add.png") forState:UIControlStateNormal];
    }
    add = NO;
}
-(void)clickButtonBegin:(CGRect)rect{
    if (bg.hidden) {
        [right setBackgroundImage:IMG(@"custom_delete_1.png") forState:UIControlStateNormal];
    }
}
-(void)clickButtonMove:(NSInteger)index view:(MyDraggableImage*)view{
    if (add) {
        for (UIView *view in [self.view subviews]) {
            if (view.tag == bg_tag) {
                view.hidden = YES;
            }
        }
    }else{
        int gap = 70;
        if (view.frame.origin.x>SCREEN_WIDTH-gap && view.frame.origin.y<gap) {
            [right setBackgroundImage:IMG(@"custom_delete_2.png") forState:UIControlStateNormal];
        }else{
            [right setBackgroundImage:IMG(@"custom_delete_1.png") forState:UIControlStateNormal];
        }
    }
}



@end






